home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!news
- From: giuliano@ix.netcom.com(Giuliano Carlini)
- Newsgroups: comp.lang.c++
- Subject: Re: Default Constructors
- Date: 13 Apr 1996 08:54:32 GMT
- Organization: Netcom
- Message-ID: <4knq48$fn6@dfw-ixnews1.ix.netcom.com>
- References: <316F2B88.5FC4@psych.stanford.edu>
- NNTP-Posting-Host: lbx-ca8-13.ix.netcom.com
- X-NETCOM-Date: Sat Apr 13 3:54:32 AM CDT 1996
-
- In <316F2B88.5FC4@psych.stanford.edu> Nick Cassimatis
- <nick@psych.stanford.edu> writes:
- >
- >I'm having trouble getting default constructors to work in another
- >class' constructor: e.g.,
- >
- >class position {
- > int x,y,z;
- > position() {x = y = z = 0;};
- > position(int, int, int);
- >};
- >
- >class object {
- > position pos;
- > object();
- >};
- >
- >object::object() {
- > pos.x = 1;
- >}
- >
- >I get the following error messages (in BC4.5):
- >
- >'position::position()' is not accessible in function object::object()
- >'position::x' is not accessible in function object::object()
- >
- >What exactly is meant by "access"? And why wouldn't I have it to an
- >object that's already been declared?
- >
- >-Nick
-
- Your problem is that the members of position - and object for that
- matter - are all private. That is, only a member's of "this" may be
- changed. That is what the error message means. The client code can't
- access the designated member. The solution is:
- class position {
- public:
- int x,y,z;
- position() {x = y = z = 0;};
- position(int, int, int);
- }
-
- Classes are often designed so that some parts are public, and therefore
- callable by clients, and other parts are protected or private, and
- therefore illegal from clients to access. Public members are part of
- the interface, protected and private part of the implementation.
-
- There is also a difference between protected and private. A protected
- member may be accessed by a subclass's function, while a private member
- may be accessed only by a function of the class, and not by a subclass.
-
- g
-